package http.aio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.util.concurrent.CountDownLatch;

/**
 * @author fengbo
 * @version 1.0.0
 * @date 2018/12/1 16:52
 * @since JDK 1.8
 */
public class AioHttpServer {

    private AsynchronousServerSocketChannel server;
    private static CountDownLatch serverStatus = new CountDownLatch(1);


    public AioHttpServer(int port) {
        init(port);
    }

    private void init(int port) {
        try {
            server = AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(port));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void listen() {
        server.accept(server, new AcceptHandler());
        try {
            serverStatus.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String... args) {
        new AioHttpServer(8080).listen();
    }
}
